// Connection class definition
// Created: Daly 2012-04-01

#include <xnt_event.h>
#include "ax_net.h"

#ifndef _AX_CONN_H_
#define _AX_CONN_H_

enum {CONN_INVALID, OUTER_CLIENT, INNER_CLIENT};

//client connection
class AxConnection 
{
public:
	AxConnection();
	AxConnection(unsigned int rsize, unsigned int wsize);
	~AxConnection();	
	void set_addr(struct sockaddr_in* addr);
	void set_handler(int which, xnt_event_proc func);

	int read();
	int send_remain();
	//command recv counter
	void add_cmd_count() { ++cmd_recv; }
	unsigned int get_cmd_count() { return cmd_recv; }
	void close();
private:
	void reset(unsigned int rsize, unsigned int wsize);

public:
	int fd;
	int type;  //outer client or intra servers
	unsigned int cmd_recv;   //command receive count from beginning
	int handle;    //handle field
	struct xnt_event_t wev;  //write event
	struct xnt_event_t rev;  //read  event
	
	//ip and port infomation
	char remote_ip[20];
	unsigned short remote_port;
	AxNetBuffer *rbuf;
	AxNetBuffer *wbuf;
	AxConnection* next;
};

//listen connection
class AxListen
{
public:
	AxListen();
	~AxListen();
	
	int listen(unsigned short port, int backend);       //begin listen

	struct xnt_event_t rev;   //only read event

	int _port;           //listen port
	int _backend;
	int _listening;
	int _fd;
};

#endif
